#include "../xml_parser.h"

//XMLParser::XMLParser(const String& name)
//: m_pCurElement(NULL)
//{
//	eve::FileHandleDataStream stream(name);
//	size_t size = stream.size();
//	char* pdata = new char[size+1];
//	pdata[size] = 0;
//	eve::MemoryDataStream memstream(pdata,size+1,true);
//	m_doc.Parse((char*)(memstream.getPtr()));
//}
//XMLParser::XMLParser(eve::MemoryDataStream& stream)
//: m_pCurElement(NULL)
//{
//	m_doc.Parse((char*)( stream.getPtr()) );
//}
//
//XMLParser::~XMLParser()
//{
//}
//
//TiXmlElement* XMLParser::SeekChild(const String& name,bool bchange)
//{
//	TiXmlElement* element = NULL;
//	if( m_pCurElement == NULL )
//	{
//		element = m_doc.FirstChildElement(name.c_str());
//	}
//	else
//	{
//		element = m_pCurElement->FirstChildElement(name.c_str());
//	}
//	if( bchange && element )
//		m_pCurElement = element;
//	return element;
//}
//
//
//TiXmlElement* XMLParser::SeekSibling(const String& name,bool bchange)
//{
//	TiXmlElement* element = NULL;
//	if( m_pCurElement == NULL )
//	{
//		element = m_doc.FirstChildElement(name.c_str());
//	}
//	else
//	{
//		element = m_pCurElement->NextSiblingElement(name.c_str());
//	}
//	if( bchange && element )
//		m_pCurElement = element;
//	return element;
//}
//
//void XMLParser::Reset()
//{
//	m_pCurElement = NULL;
//}
//
//String XMLParser::GetStringValue(const char* name)
//{
//	if( m_pCurElement == NULL || name == NULL )
//	{
//		return String();
//	}
//	else
//	{
//		const char* sz = m_pCurElement->Attribute(name);
//		if( sz )
//			return String(sz);
//		else
//			return String();
//	}
//}
TiXmlElement* LoadXMLFile(const String& name,TiXmlDocument& doc)
{
	eve::FileHandleDataStream stream(name);
	size_t size = stream.size();
	char* pdata = new char[size+1];
	stream.read(pdata,size);
	pdata[size] = 0;
	eve::MemoryDataStream memstream(pdata,size+1,true);
	doc.Parse((char*)(memstream.getPtr()));
	TiXmlElement* root = doc.FirstChildElement();
	if( root && NULL == root->NextSiblingElement() )
		return root;
	return NULL;
		
}

XMLElementWrapper::XMLElementWrapper(TiXmlElement* root)
: m_pRoot(root)
, m_pCurElement(root)
{
}

XMLElementWrapper::~XMLElementWrapper()
{
}

TiXmlElement* XMLElementWrapper::SeekChild(const String& name,bool bchange)
{
	TiXmlElement* element = NULL;
	if( m_pCurElement == NULL )
	{
		m_pCurElement = m_pRoot;
	}
	
	
	element = m_pCurElement->FirstChildElement(name.c_str());
	
	if( bchange && element )
		m_pCurElement = element;
	return element;
}


TiXmlElement* XMLElementWrapper::SeekSibling(const String& name,bool bchange)
{
	TiXmlElement* element = NULL;
	if( m_pCurElement == NULL )
	{
		m_pCurElement = m_pRoot;
	}
	
	element = m_pCurElement->NextSiblingElement(name.c_str());
	
	if( bchange && element )
		m_pCurElement = element;
	return element;
}

void XMLElementWrapper::Reset()
{
	m_pCurElement = m_pRoot;
}

String XMLElementWrapper::GetStringValue(const char* name)
{
	if( m_pCurElement == NULL || name == NULL )
	{
		return String();
	}
	else
	{
		const char* sz = m_pCurElement->Attribute(name);
		if( sz )
			return String(sz);
		else
			return String();
	}
}

String XMLElementWrapper::GetNodeText()
{
	if( m_pCurElement == NULL )
		return String();
	else
	{
		const char* sz = m_pCurElement->GetText();
		if( sz )
			return String(sz);
		else
			return String();
	}
	
}

int XMLElementWrapper::GetIntValue(const char* name)
{
	if( m_pCurElement == NULL || name == NULL )
		return -1;
	else
	{
		int value = 0;
		m_pCurElement->QueryIntAttribute(name,&value);
		return value;
	}
}

stRectI XMLElementWrapper::GetRectValue(const char* name)
{
	const String& str = GetStringValue(name);
	const size_t num = 4;
	vector<String> & ret = StringUtil::Split(str,",",num);
	if( ret.size() != num ) return stRectI();
	return stRectI(atol(ret[0].c_str()),
		atol(ret[1].c_str()),
		atol(ret[2].c_str()),
		atol(ret[3].c_str()) );
	

}

stPointI XMLElementWrapper::GetPointValue(const char* name)
{
	const String& str = GetStringValue(name);
	const size_t num = 2;
	vector<String> & ret = StringUtil::Split(str,",",num);
	if( ret.size() != num ) return stPointI();
	return stPointI(atol(ret[0].c_str()),
		atol(ret[1].c_str()) );
}

stPointI16 XMLElementWrapper::GetPoint16Value(const char* name)
{
	const String& str = GetStringValue(name);
	const size_t num = 2;
	vector<String> & ret = StringUtil::Split(str,",",num);
	if( ret.size() != num ) return stPointI16();
	return stPointI16(atol(ret[0].c_str()),
		atol(ret[1].c_str()) );
}

bool XMLElementWrapper::HasAttribute(const char* name) const
{
	const char* sz = m_pCurElement->Attribute(name);
	if( sz )
		return true;
	else 
		return false;
}